package com.ruoyi;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @Description 通用工具类
 * @ClassName CommonUtil
 */
public final class CommonUtil {

    /**
     * @desc: 将字符串转换为BufferedImage对象
     */
    public static BufferedImage createImage(String[] strs) {
        // 设置背景宽高
        //        int width = 600, height = 400;
        int width = 200, height = 100;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取图形上下文对象
        Graphics graphics = image.getGraphics();
        // 填充
        graphics.fillRect(0, 0, width, height);
        // 设定字体大小及样式
        //graphics.setFont(new Font("微软雅黑", Font.BOLD, 34));
        graphics.setFont(getSelfDefinedFont(
            "D:\\works\\student-design\\22~23project\\dev\\基于java的智能排课系统\\Course-Selection-System\\ruoyi-admin\\src\\main\\resources\\font\\字体_华文彩云.TTF"));
        // 字体颜色
        graphics.setColor(Color.BLUE);
        for (int i = 0; i < strs.length; i++) {
            // 描绘字符串
            //            graphics.drawString(strs[i], 150,  70+ (i + 1) * 50);
            graphics.drawString(strs[i], 50, 10 + (i + 1) * 50);
        }
        graphics.dispose();
        return image;
    }

    //filepath字体文件的路径
    private static java.awt.Font getSelfDefinedFont(String filepath) {
        java.awt.Font font = null;
        File file = new File(filepath);
        try {
            font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, file);
            return font.deriveFont(java.awt.Font.PLAIN, 40);
        } catch (Exception e) {
            return font;
        }
    }
}
